//
//  PayMoneyViewController.m
//  TenThousand
//
//  Created by 王 on 16/7/16.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "PayMoneyViewController.h"



@interface PayMoneyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *payMoney;//支付金额
@property (weak, nonatomic) IBOutlet UILabel *ProjectName;//产品名称
@property (weak, nonatomic) IBOutlet UILabel *Term;//期限
@property (weak, nonatomic) IBOutlet UILabel *Rate;//预期年化收益
@property (weak, nonatomic) IBOutlet UILabel *InvestmentAmount;//投资金额

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *Income;//万元收益

@property (nonatomic, strong) NSString *waterNumber;


@end

@implementation PayMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"确认投资";
    
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:NO];
    } right:nil rightClick:nil];

    [self setDataWithModel:nil];
    
    
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:NO];
    } right:nil rightClick:nil];
    
    self.ProjectName.text = [NSString stringWithFormat:@"%@%@",self.model.ProjectName,self.model.Code];
    self.Term.text = [NSString stringWithFormat:@"%@",self.model.Term];
    self.Rate.text = [NSString stringWithFormat:@"%.2f",self.model.Rate];
    self.InvestmentAmount.text = [NSString stringWithFormat:@"%@元",self.money];
//    self.Income.text = [NSString stringWithFormat:@"%@",self.Income];
   
    
    
   
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self getRecognition];
}


-(BorrowGetDetailModel *)model{
    if (!_model) {
        _model = [[BorrowGetDetailModel alloc] init];
    }
    return _model;
}

//确认投资
- (void)getRecognition{
    
    /**
     *  BrrowId 借款编号 
     *  Amount  投资金额
     *  LoginName 用户账号
     */
    NSString *LoginName = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginName"];
    
    
    NSDictionary *para = @{
                            @"LoginName":LoginName,
                            @"Amount":self.money,
                            @"BrrowId":self.model.BorrowId
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Invest.GetRecognition" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
        //返回流水号
        if ([object[@"Code"] isEqualToString:@"00000"]) {
            for (NSDictionary *dic in object[@"Data"]) {
                NSString *WaterNumber = dic[@"WaterNumber"];
                self.waterNumber = WaterNumber;
                //将返回的流水号保存到沙盒中
//                [[NSUserDefaults standardUserDefaults] setObject:WaterNumber forKey:@"WaterNumber"];
            }

        }else
            [ToastView showInfoWithStatus:object[@"Message"]];
        
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } progress:nil];
}


- (IBAction)payMoney:(id)sender {
    
    [self FengFu:self.waterNumber];
}

- (void)FengFu:(NSString *)requestId {//用户投标
    
    NSString *LoginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSDictionary *dic = @{
                          @"requestType":@"PFT0003",
                          @"requestId":requestId,
                          @"merchantCode":FengFuMerchantCode,
                          @"userIdIdentity":LoginId,
                          @"sum":self.money,//投标金额
                          @"projectCode":self.model.BorrowId,//项目编号
                          @"projectDescription":@"",//项目描述
                          @"projectSum":[NSString stringWithFormat:@"%.2f", self.model.TotalAmount],//项目总额
                          @"giftFlag":@"",//红包标识
                          @"subledgerList":@"",//分账列表
                          @"noticeUrl":@"www.baidu.com",
                          @"successReturnUrl":@"",
                          @"failReturnUrl":@"",
                          };
    NSString *sign = [[[[[[[[[[[[dic[@"requestType"] stringByAppendingString:dic[@"requestId"]] stringByAppendingString:dic[@"merchantCode"]] stringByAppendingString:dic[@"userIdIdentity"]] stringByAppendingString:dic[@"sum"]] stringByAppendingString:dic[@"projectCode"]] stringByAppendingString:dic[@"projectDescription"]] stringByAppendingString:dic[@"projectSum"]] stringByAppendingString:dic[@"giftFlag"]] stringByAppendingString:dic[@"subledgerList"]] stringByAppendingString:dic[@"noticeUrl"]] stringByAppendingString:dic[@"successReturnUrl"]] stringByAppendingString:dic[@"failReturnUrl"]];
    NSString *signature = [HMACMD5 HMACMD5WithString:sign WithKey:FengFuKEY];
    NSDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [dict setValue:signature forKey:@"signature"];
    [SumaPaySDK startService:dict callback:^(id resultDic, NSError *error) {
        NSLog(@"%@--------------%@", resultDic, error);
        if ([dic[@"result"] integerValue] == 0) {
            if ([dic[@"msg"][@"result"] integerValue] == 00000) {
                [self getSureRecognition];
            }
        }
    }];
    
}

//确认投资   去付款
-(void)getSureRecognition{
    
    
    
    
    NSString *LoginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSDictionary *para = @{
                           @"Amount":self.money,
                           @"Borrowid":self.model.BorrowId,
                           @"LoginId":LoginId,
                           @"WaterNumber":self.waterNumber
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Invest.GetSureRecognition" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } progress:nil];
}


//页面信息展示
-(void)setDataWithModel:(BorrowGetDetailModel *)model{
    _payMoney.text = [NSString stringWithFormat:@"%.2f元",[self.money floatValue]];//支付金额
    _InvestmentAmount.text = [NSString stringWithFormat:@"%.2f元",[self.money floatValue]];//投资金额
}

@end
